﻿using AnlianCommon.Common.Domains.BreedingManagement;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.SheepWeighedes
{
    public class SheepWeighedQueryHandler : IRequestHandler<SheepWeighedQueryCommand, ResultPage<SheepWeighedQueryDto>>
    {
        private readonly IBaseRepository<SheepWeighed> _sheepWeighedRepository;

        public SheepWeighedQueryHandler(IBaseRepository<SheepWeighed> sheepWeighedRepository)
        {
            _sheepWeighedRepository = sheepWeighedRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepWeighedQueryDto>> Handle(SheepWeighedQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepWeighedQueryDto>() { Code = (int)CommonErrorCode.Search };
            try
            {
                var sheepWeighed = await (from a in await _sheepWeighedRepository.GetAll()
                                          select new SheepWeighedQueryDto()
                                          {
                                              SheepWeigh = a.SheepWeigh,
                                              WeighDate = a.WeighDate,
                                              SheepNumber = a.SheepNumber,
                                              Variety = a.Variety,
                                              SheepSex = a.SheepSex,
                                              WeightType = a.WeightType,
                                              Weight = a.Weight,
                                              RecordId = a.RecordId,
                                              CreatedOwnerId = a.CreatedOwnerId,
                                              CreatedTime = a.CreatedTime,
                                          }).ToListAsync();
                if (request.StartWeighDate != null)
                {
                    sheepWeighed = sheepWeighed.Where(c => c.WeighDate >= request.StartWeighDate).ToList();
                }
                if (request.EndWeighDate != null)
                {
                    sheepWeighed = sheepWeighed.Where(c => c.WeighDate <= request.StartWeighDate).ToList();
                }
                if (!string.IsNullOrEmpty(request.SheepNumber))
                {
                    sheepWeighed = sheepWeighed.Where(c => c.SheepNumber == request.SheepNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.WeightType))
                {
                    sheepWeighed = sheepWeighed.Where(c => c.WeightType == request.WeightType).ToList();
                }
                if (!string.IsNullOrEmpty(request.Variety))
                {
                    sheepWeighed = sheepWeighed.Where(c => c.Variety == request.Variety).ToList();
                }
                if (request.SheepSex != null)
                {
                    sheepWeighed = sheepWeighed.Where(c => c.SheepSex == request.SheepSex).ToList();
                }
                result.TotalCount = sheepWeighed.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheepWeighed = sheepWeighed.OrderBy(c => c.SheepWeigh).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepWeighed;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
